﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.HeadUpWindow.CApSmartNotePictureDataContainer
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.HeadUpWindow.SmartNoteHeadUpWindow;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls;


namespace DnProcessSimulateCommands.HeadUpWindow;

public class CApSmartNotePictureDataContainer : CApSmartNoteDataContainer
{
  private const string NoteMediaPropertyName = "Media";
  private TxPictureDataContainer _picContainer;
  private int _currentfMediaFileIndex;
  private List<TxEngineeringFileData> _mediaFiles;
  private Visibility _noMediaFilevisibility = Visibility.Hidden;
  private bool _nextEnabled;
  private bool _prevEnabled;
  private bool _deleteEnabled;

  public bool NextEnabled
  {
    get => this._nextEnabled;
    set
    {
      this._nextEnabled = value;
      this.OnPropertyChanges(nameof (NextEnabled));
    }
  }

  public bool PrevEnabled
  {
    get => this._prevEnabled;
    set
    {
      this._prevEnabled = value;
      this.OnPropertyChanges(nameof (PrevEnabled));
    }
  }

  public bool DeleteEnabled
  {
    get => this._deleteEnabled;
    set
    {
      this._deleteEnabled = value;
      this.OnPropertyChanges(nameof (DeleteEnabled));
    }
  }

  public TxWindowCommand LeftCmd { get; set; }

  public TxWindowCommand RightCmd { get; set; }

  public Visibility NoMediaFileVisibility
  {
    get => this._noMediaFilevisibility;
    set
    {
      this._noMediaFilevisibility = value;
      this.OnPropertyChanges(nameof (NoMediaFileVisibility));
    }
  }

  public TxPictureDataContainer PicContainer => this._picContainer;

  public CApSmartNotePictureDataContainer()
  {
    this._picContainer = new TxPictureDataContainer();
    ((TxDataContainer) this._picContainer).PropertyChanged += new PropertyChangedEventHandler(this.TxPictureDataContainer_PropertyChanged);
    this.RightCmd = new TxWindowCommand(new Action(this.SetNextMedia));
    this.LeftCmd = new TxWindowCommand(new Action(this.SetPrevMedia));
  }

  private void TxPictureDataContainer_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName == "PictureTitle")
    {
      this.OnPropertyChanges("PictureBy");
    }
    else
    {
      if (!(e.PropertyName == "LastModifiedDate"))
        return;
      this.OnPropertyChanges("LastModifiedDateStr");
    }
  }

  public string PictureBy => "";

  public int NumberOfMedia
  {
    get
    {
      List<TxEngineeringFileData> mediaFiles = this._mediaFiles;
      return mediaFiles == null ? 0 : __nonvirtual (mediaFiles.Count);
    }
  }

  public string LastModifiedDateStr
  {
    get
    {
      if (!this.PicContainer.LastModifiedDate.HasValue)
        return "";
      string[] strArray = new string[3]
      {
        SmartNoteHUDWindowStringTable.PICTURE_CREATED + ":",
        null,
        null
      };
      DateTime dateTime = this.PicContainer.LastModifiedDate.Value;
      strArray[1] = dateTime.ToShortDateString();
      dateTime = this.PicContainer.LastModifiedDate.Value;
      strArray[2] = dateTime.ToShortTimeString();
      return string.Join(" ", strArray);
    }
  }

  public override void Load(TxNote note)
  {
    base.Load(note);
    object obj = (object) "";
    this._currentfMediaFileIndex = 0;
    this._picContainer.SetDefaultImage(new Uri("pack://application:,,,/DnProcessSimulateCommands;component/HeadUpWindow/Resources/MissingImage.png"));
    if (TxGenericNoteEx.TryGetNoteProperty(note, "Media", ref obj, ""))
      this._mediaFiles = obj as List<TxEngineeringFileData>;
    this.UpdateMedia();
  }

  public string MediaNumberOutOfStr
  {
    get => this.MediaNumberString;
    set => this.OnPropertyChanges(nameof (MediaNumberOutOfStr));
  }

  public void SetNextMedia()
  {
    if (!this.NextEnabled)
      return;
    ++this._currentfMediaFileIndex;
    this.UpdateMedia();
  }

  public void SetPrevMedia()
  {
    if (!this.PrevEnabled)
      return;
    --this._currentfMediaFileIndex;
    this.UpdateMedia();
  }

  private void UpdatePendingMedia(List<TxEngineeringFileData> files)
  {
    if (files.Count > 0)
    {
      this.PendingProperties["Media"] = (object) files;
    }
    else
    {
      this.PendingProperties.Remove("Media");
      this.PendingRemoveProperties.Add("Media");
    }
  }

  public void DeleteCurrentMedia()
  {
    this._mediaFiles.RemoveAt(this._currentfMediaFileIndex);
    this.UpdatePendingMedia(this._mediaFiles);
    if (this._currentfMediaFileIndex > 0)
      --this._currentfMediaFileIndex;
    this.UpdateMedia();
  }

  private void UpdateMedia()
  {
    if (this._mediaFiles != null && this._mediaFiles.Count > 0)
    {
      this._picContainer.Load(this._mediaFiles[this._currentfMediaFileIndex].FullPath);
    }
    else
    {
      this._picContainer.ApplyDefault();
      this.NoMediaFileVisibility = Visibility.Visible;
    }
    int currentfMediaFileIndex = this._currentfMediaFileIndex;
    List<TxEngineeringFileData> mediaFiles1 = this._mediaFiles;
    // ISSUE: explicit non-virtual call
    int num = (mediaFiles1 != null ? __nonvirtual (mediaFiles1.Count) : 0) - 1;
    this.NextEnabled = currentfMediaFileIndex < num;
    this.PrevEnabled = this._currentfMediaFileIndex > 0;
    List<TxEngineeringFileData> mediaFiles2 = this._mediaFiles;
    // ISSUE: explicit non-virtual call
    this.DeleteEnabled = mediaFiles2 != null && __nonvirtual (mediaFiles2.Count) != 0;
    this.MediaNumberOutOfStr = this.MediaNumberString;
    this.UpdateApply();
  }

  public string MediaNumberString
  {
    get
    {
      List<TxEngineeringFileData> mediaFiles = this._mediaFiles;
      int count = mediaFiles != null ? __nonvirtual (mediaFiles.Count) : 0;
      return $"{(count > 0 ? this._currentfMediaFileIndex + 1 : 0).ToString()}/{count.ToString()}";
    }
  }
}
